class Base{
    isEmpty(param){
        //参数  是对象  数组
        if(Array.isArray(param)){
            //数组
            return param.length ? `该数组有${param.length}项数据` : '此数组为空';
        }else{
            //对象
            let keys = Object.keys(param);  //[]
            return keys.length ? `该对象有${keys.length}项数据` : '此对象为空';

        }
    }
}

const fs = require('fs');

const path = require('path');

//nodejs 内置模块  遵循的是commonjs规范

//require() 引入模块   module.exports 来抛出模块

class Util extends Base{
    constructor(options) {
        super(options)
    }

    loadJs(path){
        return this.readFile(path)
    }

    loadCss(path){
        return this.readFile(path)
    }


    loadImg(path){
        return this.readFile(path,'base64')    
    }

    //读取文件
    readFile(path,type='utf8'){
        //读取文件是一个异步 readFile
        return new Promise((resolve,reject) => {
            fs.readFile(path,type,(error,data) => {
                if(error){
                    reject(error)
                }else{
                    resolve(data);
                }
            })
        })
    }

    async init(files){
        let arr = [];
        for(let i = 0;i<files.length;i++){
           let extname =  path.extname(files[i]).slice(1);
           console.log(extname);
        //    if(extname === 'js'){
        //        this.loadJs(files[i])
        //    }else if(extname === 'css'){
        //        this.loadCss(files[i])
        //    }else{
        //        this.loadImg(files[i])
        //    }

        extname = /^(jpg|png)$/.test(extname) ? 'img' : extname;

         let type = 'load'+extname.substr(0,1).toUpperCase()+extname.substr(1);
         console.log(type)
        //  this[type](files[i])  this.type
            let data = await this[type](files[i]);
            arr.push(data);
        }
        return arr
    }
    
}


let util = new Util();

console.log(util.isEmpty([]))

// util.init(['./src/index.js','./src/index.css','./src/1.jpg']).then(res => {
//     console.log(res)
// })

//类  都有一个prototype的属性

//实例 __proto__

console.log(Base.prototype);

let b = new Base();

console.log(b.__proto__ === Base.prototype)

console.log(b);